﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Navigation;
using wp8nop.Common;
using wp8nop.Model;

namespace wp8nop.ViewModel
{
    public class ProdottiListViewModel : ViewModelBase
    {
        public event EventHandler<ProdottoViewModel> OnOpenDettaglioProdotto;
        int Page;

        private readonly IDataService _dataService;
        public ProdottiListViewModel(IDataService data)
        {
            _dataService = data;
        }

        internal void OpenDettaglioProdotto(ProdottoViewModel prod)
        {
            if (OnOpenDettaglioProdotto != null)
                OnOpenDettaglioProdotto(this, prod);
        }

        /// <summary>
        /// The <see cref="IsLoading" /> property's name.
        /// </summary>
        public const string IsLoadingPropertyName = "IsLoading";

        private bool _isLoading = false;

        /// <summary>
        /// Sets and gets the IsLoading property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsLoading
        {
            get
            {
                return _isLoading;
            }

            set
            {
                if (_isLoading == value)
                {
                    return;
                }

                RaisePropertyChanging(IsLoadingPropertyName);
                _isLoading = value;
                RaisePropertyChanged(IsLoadingPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="VisibilityLoading" /> property's name.
        /// </summary>
        public const string VisibilityLoadingPropertyName = "VisibilityLoading";

        private Visibility _visibilityLoading = Visibility.Collapsed;

        /// <summary>
        /// Sets and gets the VisibilityLoading property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public Visibility VisibilityLoading
        {
            get
            {
                return _visibilityLoading;
            }

            set
            {
                if (_visibilityLoading == value)
                {
                    return;
                }

                RaisePropertyChanging(VisibilityLoadingPropertyName);
                _visibilityLoading = value;
                RaisePropertyChanged(VisibilityLoadingPropertyName);
            }
        }
        /// <summary>
        /// The <see cref="SelectedProduct" /> property's name.
        /// </summary>
        public const string SelectedProductPropertyName = "SelectedProduct";

        private ProdottoViewModel _selectedProduct = null;

        /// <summary>
        /// Sets and gets the SelectedProduct property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ProdottoViewModel SelectedProduct
        {
            get
            {
                return _selectedProduct;
            }

            set
            {
                if (_selectedProduct == value)
                {
                    return;
                }

                RaisePropertyChanging(SelectedProductPropertyName);
                _selectedProduct = value;
                RaisePropertyChanged(SelectedProductPropertyName);
                if (SelectedProduct != null)
                    OpenDettaglioProdotto(SelectedProduct);
                
            }
        }
        public Categoria SelectedCategory { get; set; }
        /// <summary>
        /// The <see cref="Prodotti" /> property's name.
        /// </summary>
        public const string ProdottiPropertyName = "Prodotti";

        private ObservableCollection<ProdottoViewModel> _prodotti = null;

        /// <summary>
        /// Sets and gets the Prodotti property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<ProdottoViewModel> Prodotti
        {
            get
            {
                if (_prodotti == null)
                    _prodotti = new ObservableCollection<ProdottoViewModel>();
                return _prodotti;
            }

            set
            {
                if (_prodotti == value)
                {
                    return;
                }

                RaisePropertyChanging(ProdottiPropertyName);
                _prodotti = value;
                RaisePropertyChanged(ProdottiPropertyName);
            }
        }

        public void LoadProducts()
        {
            try
            {
                if (IsLoading)
                    return;
                IsLoading = true;
                VisibilityLoading = Visibility.Visible;

                SelectedProduct = null;
                Debug.WriteLine("Get prodotti ");
                _dataService.GetProducts(
                    (item, error) =>
                    {
                        IsLoading = false;
                        VisibilityLoading = Visibility.Collapsed;

                        Debug.WriteLine("Ritorno callback");
                        if (error != null)
                        {
                            Toast.SendToast("Verifica la tua connessione internet");
                            return;
                        }

                        Debug.WriteLine("Dopo check error");
                        if( item == null )
                            Debug.WriteLine("item è null");

                        Debug.WriteLine("Get prodotti " + item.Count.ToString());


                        item.ForEach( x => Prodotti.Add( x ));

                    }, SelectedCategory, SearchString, Page++);
            }
            catch (System.Exception ex)
            {

                Debug.WriteLine("Eccezione " + ex.Message);
            }
        }

        internal void ClearProducts()
        {
            try
            {
                Page = 0;
                Prodotti.Clear();
            }
            catch (Exception ex)
            {

                Debug.WriteLine("Eccezione " + ex.Message);
            }
        }

        public string SearchString { get; set; }
    }
}
